﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLNHK.BLL;

namespace QLNHK.DANHMUC
{
    public partial class frmDMToDanPho : Form
    {
        private DataSet dsDM;
        private ToDanPho objDM = new ToDanPho();
        private int _ID = 0;

        #region Set Authentication On Form
        private cls_Role ObjRole = null;
        private Cls_PHANQUYEN_NHOM ObjPhanQuyen = null;
        private Infragistics.Win.DefaultableBoolean GetStatus(bool values)
        {
            if (values)
            {
                return Infragistics.Win.DefaultableBoolean.True;
            }
            else
            {
                return Infragistics.Win.DefaultableBoolean.False;
            }
        }
        private void SetAuthenticationOnForm()
        {
            try
            {
                if (!frmMain.ObjNguoiDung.IsAdministrator)
                {
                    ObjRole = new cls_Role();
                    ObjPhanQuyen = new Cls_PHANQUYEN_NHOM();

                    ObjRole = new cls_Role(Global.GetRole(ObjPhanQuyen.GetRoleByIDChucNang(frmMain.ObjNguoiDung.ID_NguoiDung, this.Tag.ToString())));

                    if (ObjRole != null)
                    {
                        if (ObjRole.HienThi == false)//không hiển thị
                        {
                            foreach (Control ctl in this.Controls)
                            {
                                ctl.Visible = false;
                            }
                        }
                        //chỉ cho phép hiển thị, không cho phép cập nhật, sửa, xóa
                        else if (ObjRole.HienThi == true && ObjRole.Them == false && ObjRole.Sua == false && ObjRole.Xoa == false)
                        {
                            //Grid tom tat ban than
                            grdMain.DisplayLayout.Bands[0].Override.AllowDelete = GetStatus(ObjRole.Xoa);
                            grdMain.DisplayLayout.Bands[0].Override.AllowUpdate = GetStatus(ObjRole.Sua);


                            //btnAdd.Enabled = ObjRole.Them;
                            btnUpdate.Enabled = ObjRole.Sua;
                            btnDelete.Enabled = ObjRole.Xoa;
                        }
                        //cho phép hiển thị, cho phép hoặc cập nhật, hoặc sửa, hoặc xóa
                        else
                        {
                            CheckAuthentication();
                        }
                    }
                }

                btnClose.Enabled = true;
            }

            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
            }
        }
        private void CheckAuthentication()
        {
            try
            {
                if (ObjRole != null)
                {
                    //Grid tom tat ban than
                    grdMain.DisplayLayout.Bands[0].Override.AllowDelete = GetStatus(ObjRole.Xoa);
                    btnDelete.Enabled = ObjRole.Xoa;

                    grdMain.DisplayLayout.Bands[0].Override.AllowUpdate = GetStatus(ObjRole.Sua);
                    btnUpdate.Enabled = ObjRole.Sua;
                }
            }
            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
            }
        }
        #endregion

        public frmDMToDanPho()
        {
            InitializeComponent();
        }
        private void loadDataToGrid()
        {
            dsDM = ToDanPho.SelectDynamic("IDPhuongXa=" + cboXaPhuong.Value, "");
            grdMain.DataSource = dsDM;
            ultraGroupBox3.Text = "Danh sách Tổ dân phố của " + cboXaPhuong.Text;
        }
        private void frmDMXaPhuong_Load(object sender, EventArgs e)
        {
            Global.setDataToComboThanhPho(cboThanhPho,false);
            this.SetAuthenticationOnForm();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            switch (btnUpdate.Tag.ToString())
            {
                case "INSERT":
                    objDM = new ToDanPho();
                    objDM.IDPhuongXa = Convert.ToInt32(cboXaPhuong.Value);
                    objDM.Ten = txtTen.Text;
                    objDM.Loai = txtLoai.Text;
                    objDM.Insert();

                    txtTen.Text = "";
                    txtLoai.Text = "";
                    this.loadDataToGrid();
                    break;
                case "UPDATE":
                    objDM.ID = _ID;
                    objDM.IDPhuongXa = Convert.ToInt32(cboXaPhuong.Value);
                    objDM.Ten = txtTen.Text.Trim();
                    objDM.Loai = txtLoai.Text.Trim();
                    objDM.Update();
                    this.loadDataToGrid();
                    btnUpdate.Tag = "INSERT";
                    btnUpdate.Text = "Thêm mới";
                    txtTen.Text = "";
                    txtLoai.Text = "";
                    grdMain.Enabled = true;
                    break;
            }
        }

        private void grdMain_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs e)
        {
            _ID = Convert.ToInt32(e.Row.Cells["ID"].Value);
            cboXaPhuong.Value = e.Row.Cells["IDPhuongXa"].Value;
            txtTen.Text = e.Row.Cells["Ten"].Text;
            txtLoai.Text = e.Row.Cells["Loai"].Text;
            btnUpdate.Tag = "UPDATE";
            btnUpdate.Text = "Cập nhật";
            grdMain.Enabled = false;
        }

        private void grdMain_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            grdMain.DisplayLayout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.ResizeAllColumns;
            grdMain.DisplayLayout.Bands[0].Columns["ID"].Hidden = true;
            grdMain.DisplayLayout.Bands[0].Columns["IDPhuongXa"].Hidden = true;

            grdMain.DisplayLayout.Bands[0].Columns["Ten"].Header.Caption = "Tên Tổ dân phố";
            grdMain.DisplayLayout.Bands[0].Columns["Loai"].Header.Caption = "Loại";
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            objDM.ID = Convert.ToInt32(grdMain.ActiveRow.Cells["ID"].Value);
            DialogResult result = MessageBox.Show("Bạn có muốn xóa " + grdMain.ActiveRow.Cells["Ten"].Value.ToString() + " ?\nChọn YES để xóa, NO để hủy.", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                objDM.Delete();
                this.loadDataToGrid();
            }
        }

        private void grdMain_BeforeRowsDeleted(object sender, Infragistics.Win.UltraWinGrid.BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            e.Cancel = true;
        }

        private void cboThanhPho_ValueChanged(object sender, EventArgs e)
        {
            if (cboThanhPho.Value != null)
            {
                int cboThanhPhoValue = 0;
                Int32.TryParse(cboThanhPho.Value.ToString(), out cboThanhPhoValue);
                if (cboThanhPhoValue > 0)
                    Global.setDataToComboQuanHuyen(cboQuanHuyen, Convert.ToInt32(cboThanhPho.Value),false);
            }
            
        }

        private void cboQuanHuyen_ValueChanged(object sender, EventArgs e)
        {
            if (cboQuanHuyen.Value != null)
            {
                int cboQuanHuyenValue = 0;
                Int32.TryParse(cboQuanHuyen.Value.ToString(), out cboQuanHuyenValue);
                if (cboQuanHuyenValue > 0)
                    Global.setDataToComboXaPhuong(cboXaPhuong, Convert.ToInt32(cboQuanHuyen.Value),false);
            }
        }

        private void cboXaPhuong_ValueChanged(object sender, EventArgs e)
        {
            if (cboXaPhuong.Value != null)
            {
                int cboXaPhuongValue = 0;
                Int32.TryParse(cboXaPhuong.Value.ToString(), out cboXaPhuongValue);
                if (cboXaPhuongValue > 0)
                    this.loadDataToGrid();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
